#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

/* Windows */
GtkWidget* main_window;
/* Windows widgets */
GtkWidget* box_window;
GtkWidget* scroll_message;
GtkWidget* view_message;
GtkWidget* box_message;
GtkWidget* entry_message;
GtkWidget* button_send;

void display_UI();
void send_message(GtkWidget* widget G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED);

int main(int argc, char** argv)
{	
	/* GTK init */
	gtk_init(&argc, &argv);
	
	/* Display user interface, what else ? */
	display_UI();

	/* Launching GTK events loop */
	gtk_main();
	
	return 0;
}

void display_UI()
{
	/* Main windows */
	main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(main_window), "Panda messenger");
	gtk_window_set_default_size(GTK_WINDOW(main_window), 400, 300);

	/* Close button -> quit */
	g_signal_connect(G_OBJECT(main_window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
	
	/* Widgets */
	box_window    = gtk_vbox_new(FALSE, 0);
	
	/* Message view */
	view_message   = gtk_text_view_new();
	gtk_text_view_set_editable(GTK_TEXT_VIEW(view_message), FALSE);
	gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(view_message), GTK_WRAP_WORD);
	
	/* with scroll bars */
	scroll_message = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll_message), view_message);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_message),
	                               GTK_POLICY_AUTOMATIC,
	                               GTK_POLICY_AUTOMATIC);
	
	box_message   = gtk_hbox_new(FALSE, 0);
	entry_message = gtk_entry_new();
	button_send   = gtk_button_new_with_label(" ↵ ");
	
	/* Adding message entry + button to box widget */
	gtk_box_pack_start(GTK_BOX(box_message), entry_message, TRUE, TRUE, 1);
	gtk_box_pack_start(GTK_BOX(box_message), button_send, FALSE, FALSE, 1);	

	/* Adding widgets to windows */
	gtk_box_pack_start(GTK_BOX(box_window), scroll_message, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(box_window), box_message, FALSE, TRUE, 1);
	gtk_container_add(GTK_CONTAINER(main_window), box_window);
	
	/* Signal to send message (enter or send button)*/
	g_signal_connect(G_OBJECT(entry_message), "activate", G_CALLBACK(send_message), NULL);	
	g_signal_connect(G_OBJECT(button_send), "clicked", G_CALLBACK(send_message), NULL);
	
	/* Some fake text */
	GtkTextBuffer* buffer;
	GtkTextIter current;
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view_message));
	gtk_text_buffer_get_end_iter(buffer, &current);
	gtk_text_buffer_insert(buffer, &current, "* Welcome to Panda messenger !\n", -1);
	gtk_text_buffer_insert(buffer, &current, "<-- Connecting to PANDA Network... \n", -1);
	/* ------------- */
	
	/* Focus on the message entry */
	gtk_widget_grab_focus(entry_message);
	
	/* Windows display */
	gtk_widget_show_all(main_window);
}

void send_message(GtkWidget* widget G_GNUC_UNUSED, gpointer data G_GNUC_UNUSED)
{
	/* Messages buffer */
	GtkTextBuffer* buffer;
	GtkTextIter    current;
	const gchar*   message;
	gchar*         display;
	/* Time */
	GDateTime*     datetime;
	gchar*         datestr;
	
	/* Getting current iter of text */
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view_message));
	gtk_text_buffer_get_end_iter(buffer, &current);
	
	/* Writing message */
	message = gtk_entry_get_text(GTK_ENTRY(entry_message));
	
	if (*message != 0)
	{
		datetime = g_date_time_new_now_local();
		datestr  = g_date_time_format(datetime, "%H:%M:%S");
		
		display = g_strdup_printf("[%s] User@Other : %s\n", datestr, message);
		gtk_text_buffer_insert(buffer, &current, display, -1);
		
		g_free(display);
		g_date_time_unref(datetime);
		g_free(datestr);
	}
	
	/* Cleaning message input */
	gtk_entry_set_text(GTK_ENTRY(entry_message), "");
}

